package com.scau.yangsuiyu.estore.controller.seller.manageShop;

import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.dababases.entity.Shop;
import com.scau.yangsuiyu.estore.service.CreateShopService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 创建店铺界面
 * @date 2019-06-08 15:33
 * @email suiyu_yang@163.com
 */
public class CreateShopPane extends AnchorPane implements Initializable {
    private Seller seller;
    private ManageSellerShop manageSellerShop;

    @FXML
    private JFXTextArea shopNameTA;

    @FXML
    private JFXTextField businessLicenseTF;

    @FXML
    private TextArea profileTA;

    public CreateShopPane(Seller seller, ManageSellerShop manageSellerShop) {
        this.seller = seller;
        this.manageSellerShop = manageSellerShop;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/seller/manageShop/CreateShopPane.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    private void registerShop() {
        if (shopNameTA.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入店铺名！").showAndWait();
        } else if (businessLicenseTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入营业执照编号！").showAndWait();
        } else if (profileTA.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入店铺简介！").showAndWait();
        } else {
            CreateShopService createShopService = new CreateShopService(getCurrentShop());
            JSONObject resultJson = createShopService.registerShop();

            new Alert(Alert.AlertType.INFORMATION, resultJson.getString("result")).showAndWait();
            if (resultJson.getString("status").equals("success")) {
                seller.setShop(resultJson.getObject("shop", Shop.class));
                manageSellerShop.returnMainPane();
            }
        }
    }

    /**
     * 获取当前正准备注册的Shop类
     *
     * @return
     */
    private Shop getCurrentShop() {
        Shop shop = new Shop();

        shop.setProfile(profileTA.getText());
        shop.setShopName(shopNameTA.getText());
        shop.setBusinessLicense(businessLicenseTF.getText());
        shop.setSeller(seller);

        return shop;
    }

    /**
     * 重置当前的TextField和TextArea
     */
    private void resetTFAndTA() {
        shopNameTA.setPromptText("请输入店铺名");
        businessLicenseTF.setPromptText("请输入营业执照编号");
        profileTA.setPromptText("请输入您的店铺简介");
    }
}
